home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-11-30 | 6.1 KB | 281 lines | [TEXT/KAHL] |
- /***********************************************************************************
- CExpanderButton.h
-
- Copyright © 1994 B-Ray Software. All rights reserved.
- Developed using Symantec C++ 7.0.2 and Symantec's TCL library.
- Portions of this code courtesy Symantec, Inc.
-
- This code may be freely distributed as long as this notice remains. This code
- may not be used in any commercial software without the consent of B-Ray Software.
-
- ---
-
- CExpanderButton is a pane that shows a toggle button much like that found in
- the Apple's Finder file list and Symantec's Project Manager project window.
- The class supports both color and B&W icons.
-
- ***********************************************************************************/
- #include <TBUtilities.h>
-
- #include "CExpanderButton.h"
- #include "ExpanderMessages.h"
- #include "CExpander.h"
-
-
- #define CICN_BASE 5000 // hardcoded resource ID of ICON's to show - sorry!
-
-
- TCL_DEFINE_CLASS_D1( CExpanderButton, CExpanderPane );
-
-
- /*
- * NumInstances keeps track of the number of times we have invoked this class. We use it
- * to determine when to create and destroy the icons structure. Otherwise we would have
- * a bunch of them, all the same for each invokation
- */
-
- short CExpanderButton::numInstances = 0;
- CIconHandle CExpanderButton::icons[ kMaxIconIndex ];
-
-
- /*
- * CExpanderButton constructor
- *
- * Default constructor - should only be called when created by a file read.
- */
-
- CExpanderButton :: CExpanderButton() : CExpanderPane()
- {
- IExpanderButton( TRUE ); // obtain our icon handles
-
- TCL_END_CONSTRUCTOR
- }
-
-
- /*
- * CExpanderButton constructor
- *
- * Normal constructor - should always be called when creating a new
- * CExpanderButton object.
- */
-
- CExpanderButton :: CExpanderButton( CView *anEnclosure, CBureaucrat *aSupervisor, short aWidth, short aHeight,
- short aHLoc, short aVLoc, SizingOption aHSizing, SizingOption aVSizing,
- Boolean useColor )
- : CExpanderPane( anEnclosure, aSupervisor, aWidth, aHeight, aHLoc, aVLoc,
- aHSizing, aVSizing )
- {
- IExpanderButton( useColor ); // obtain our icon handles
-
- TCL_END_CONSTRUCTOR
- }
-
-
- /*
- * CExpanderButton destructor
- *
- * Frees memory used by our icon handles.
- */
-
- CExpanderButton :: ~CExpanderButton()
- {
- TCL_START_DESTRUCTOR
-
- --numInstances;
-
- if ( usingColor == TRUE && numInstances == 0 ) {
- short loop;
- for ( loop = 0; loop < kTwirly; ++loop )
- if ( icons[ loop ] )
- DisposeCIcon( icons[ loop ] );
- }
- }
-
-
- /*
- * IExanderButton method
- *
- * Private method to setup our button's icons.
- */
-
- void CExpanderButton :: IExpanderButton( Boolean useColor )
- {
- SetWantsClicks( TRUE ); // we want user clicks
-
- if ( gSystem.hasColorQD && useColor ) {
- short loop, id;
- usingColor = TRUE;
-
- /*
- * Load color icons if this is the first class object created
- */
- if ( ++numInstances == 1 ) {
- for ( loop = 0; loop < kMaxIconIndex; ++loop ) {
- id = CICN_BASE + loop;
- icons[ loop ] = GetCIcon( id );
- }
- }
- }
- else {
- usingColor = FALSE;
- }
-
- value = 0;
- }
-
-
- /*
- * SetEnabled method
- *
- * Enables/disables our button. Display of button should change with
- * this state change.
- */
-
- void CExpanderButton :: SetEnabled( Boolean fEnabled )
- {
- SetWantsClicks( fEnabled ); // disable selection by mouse
- Prepare();
- Refresh(); // cause update to redraw our button
- }
-
-
- /*
- * SetValue method
- *
- * Changes the value of our button - expanded or unexpanded depending on
- * the value given.
- */
-
- void CExpanderButton :: SetValue( Boolean aValue )
- {
- value = aValue; // save new value
- Prepare();
- Refresh(); // cause update to redraw our button
- }
-
-
- /*
- * DrawIcon method
- *
- * Protected method that draws our icon in the pane.
- */
-
- void CExpanderButton :: DrawIcon( short index )
- {
- Rect iconRect = { 0, 0, 16, 16 };
-
- // EraseRect( &iconRect ); // not needed if drawing done in copy mode
- if ( usingColor ) {
- PlotCIcon( &iconRect, icons[ index - 1 ] );
- }
- else {
- DrawSICN( CICN_BASE, index, topLeft( iconRect ) );
- }
- }
-
-
- /*
- * Draw method - OVERRIDE
- *
- * Called in response to an update event in our pane.
- */
-
- void CExpanderButton :: Draw( Rect *area )
- {
- if ( ! printing ) // don't draw icon when printing - laser printers see it anyway
- DrawIcon( CalcIconIndex() );
- }
-
-
- /*
- * DoClick method - OVERRIDE
- *
- * Handles mouse down events in the pane. We watch the mouse until the button is
- * released.
- */
-
- void CExpanderButton :: DoClick( Point hitPt, short modifierKeys, long when )
- {
- Boolean wasInButton = TRUE;
- Point where;
- Rect btnFrame;
- long dummy;
-
- Prepare();
-
- LongToQDRect( &frame, &btnFrame ); // get frame in QD space
- DrawIcon( CalcIconIndex() + 1 ); // draw the button in hilited state
-
- while ( StillDown() ) { // loop until the button is released
- GetMouse( &where );
- if ( PtInRect( where, &btnFrame ) ) {
- if ( !wasInButton ) { // mouse entered button
- DrawIcon( CalcIconIndex() + 1 );
- wasInButton = TRUE;
- }
- }
- else {
- if ( wasInButton ) { // mouse left button
- DrawIcon( CalcIconIndex() );
- wasInButton = FALSE;
- }
- }
- };
-
- if ( wasInButton ) { // button released inside pane
- Delay( 2, &dummy ); // wait a little bit
- EraseRect( &btnFrame );
- DrawIcon( kTwirly ); // draw an intermediate icon to show movement
- Delay( 2, &dummy ); // wait a little bit more
- EraseRect( &btnFrame );
- DoCommand( value ? cmdUnexpandPane : cmdExpandPane ); // send command for parent to see
- }
- }
-
-
- /*
- * CalcIconIndex method
- *
- * Private method that calculates the appropriate icon index to use when drawing.
- * Takes into account enabled state and present value of button.
- */
-
- short CExpanderButton :: CalcIconIndex( void )
- {
- short index = kUnexpandedDisabled;
-
- if ( GetWantsClicks() ) // enabled?
- ++index;
-
- if ( value ) // selected?
- index += 3;
-
- return index;
- }
-
-
- /*
- * PutTo method - OVERRIDE
- *
- * Writes to the stream all of the info we need to save.
- */
-
- void CExpanderButton :: PutTo( CStream &stream )
- {
- stream << value;
- CExpanderPane::PutTo( stream );
- }
-
-
- /*
- * GetFrom method - OVERRIDE
- *
- * Reads from the stream all of the info that we saved.
- */
-
- void CExpanderButton :: GetFrom( CStream &stream )
- {
- stream >> value;
- CExpanderPane::GetFrom( stream );
- }
-